package com.hula.core.websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.NettyRuntime;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;


/**
 * @author nyh
 */
@Slf4j
@Configuration
public class NettyWebSocketServer {
	// 监听端口
	public static final int WEB_SOCKET_PORT = 8090;
	// 主线程池执行器
	private final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
	// 子线程池执行器
	private final EventLoopGroup workerGroup = new NioEventLoopGroup(NettyRuntime.availableProcessors());

	/**
	 * 启动 ws server
	 */
	@PostConstruct
	public void start() throws InterruptedException {
		run();
	}

	/**
	 * 销毁
	 */
	@PreDestroy
	public void destroy() {
		bossGroup.shutdownGracefully().syncUninterruptibly();
		workerGroup.shutdownGracefully().syncUninterruptibly();
		log.info("WS Server关闭成功");
	}

	public void run() throws InterruptedException {
		// 服务器启动引导对象
		ServerBootstrap serverBootstrap = new ServerBootstrap();
		serverBootstrap.group(bossGroup, workerGroup)
				.channel(NioServerSocketChannel.class)
				.option(ChannelOption.SO_BACKLOG, 128)
				.option(ChannelOption.SO_KEEPALIVE, true)
				// 为 bossGroup 添加 日志处理器
				.handler(new LoggingHandler(LogLevel.INFO))
				.childHandler(new NettyChannelInitializer());
		// 启动服务器，监听端口，阻塞直到启动成功
		serverBootstrap.bind(WEB_SOCKET_PORT).sync();
	}

}
